﻿using System;
using System.Text;
using System.Web;

namespace Unknown.Example.Entity
{
    /// <summary>
    /// 共享类
    /// </summary>
    public static class Share
    {
        public const string SessionDojoThemeKey = "DojoTheme";

        /// <summary>
        /// 根路径
        /// </summary>
        public static string Root { get; set; }
        /// <summary>
        /// 根目录
        /// </summary>
        public static string Directory { get; set; }
        /// <summary>
        /// Dojo资源
        /// </summary>
        public static string DojoResource { get; set; }
        /// <summary>
        /// 自定义资源
        /// </summary>
        public static string CustomResource { get; set; }

        /// <summary>
        /// Javascript标签
        /// </summary>
        /// <param name="url">路径</param>
        /// <returns>结果</returns>
        private static string JavascriptTag(string url)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(url))
            {
                result = string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", url);
            }

            return result;
        }

        /// <summary>
        /// Link标签
        /// </summary>
        /// <param name="url">路径</param>
        /// <returns>结果</returns>
        private static string LinkTag(string url)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(url))
            {
                result = string.Format("<link rel=\"stylesheet\" href=\"{0}\" />", url);
            }

            return result;
        }

        /// <summary>
        /// 初始化Dojo资源
        /// </summary>
        private static void InitializeDojoResource()
        {
            StringBuilder dojoBuilder = new StringBuilder();

            string enter = Environment.NewLine;

            dojoBuilder.Append(enter);
            dojoBuilder.Append(Share.LinkTag(string.Format("{0}Resource/Javascript/dojo/1.10.4/dojo/resources/dojo.css", Share.Root)));
            dojoBuilder.Append(enter);
            dojoBuilder.Append(Share.LinkTag(string.Format("{0}Resource/Javascript/dojo/1.10.4/dijit/themes/dijit.css", Share.Root)));
            dojoBuilder.Append(enter);

            string[] themes = { "claro", "nihilo", "soria", "tundra" };
            foreach (string theme in themes)
            {
                dojoBuilder.Append(Share.LinkTag(string.Format("{0}Resource/Javascript/dojo/1.10.4/dijit/themes/{1}/{1}.css", Share.Root, theme)));
                dojoBuilder.Append(enter);
            }

            dojoBuilder.Append("<script type=\"text/javascript\">");
            dojoBuilder.Append(@"
    dojoConfig = {
        parseOnLoad: true
    };");
            dojoBuilder.Append(enter);
            dojoBuilder.Append("</script>");
            dojoBuilder.Append(enter);
            dojoBuilder.Append(Share.JavascriptTag(string.Format("{0}Resource/Javascript/dojo/1.10.4/dojo/dojo.js", Share.Root)));

            Share.DojoResource = dojoBuilder.ToString();
        }

        /// <summary>
        /// 初始化自定义资源
        /// </summary>
        private static void InitializeCustomResource()
        {
            StringBuilder customBuilder = new StringBuilder();

            string enter = Environment.NewLine;

            customBuilder.Append(enter);
            customBuilder.Append(Share.LinkTag(string.Format("{0}Resource/Javascript/Custom/Share.css", Share.Root)));

            Share.CustomResource = customBuilder.ToString();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="context">上下文</param>
        public static void Initialize(HttpContext context)
        {
            if (context != null)
            {
                if (context.Request != null)
                {
                    Share.Root = context.Request.ApplicationPath;
                    Share.Directory = context.Request.PhysicalApplicationPath;

                    if (!string.IsNullOrEmpty(Share.Root))
                    {
                        int index = Share.Root.Length - 1;
                        if (Share.Root[index] != '/')
                        {
                            Share.Root += "/";
                        }
                    }

                    Share.InitializeDojoResource();
                    Share.InitializeCustomResource();
                }
            }
        }
    }
}
